import axios from 'axios'
import { useCounterStore } from '../stores/counter'
const instance = new axios.create({
    baseURL: 'http://localhost:8080/api',
    timeout: 1000,
    headers: {
        'Content-Type' : 'application/json'
    }
})

instance.interceptors.request.use(config => {
// Do something before request is sent
const counterStore = useCounterStore()
config.headers['token'] = counterStore.count
return config;
},error => {
// Do something with request error
return Promise.reject(error);
});

instance.interceptors.response.use(response => {
// Do something before response is sent

return response.data;
},error => {
     if (error.code === 'ECONNABORTED' || error.message === 'Network Error') {
         ElMessage.error('请求失败，请稍后重试')
    } else {
        ElMessage.error('服务器请求失败')
    }

// Do something with response error
return Promise.reject(error);
});

export default instance
